
package com.daily.videoedit.dao;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.daily.videoedit.app.App;
import com.daily.videoedit.model.User;
import com.xunxi.mimi.utils.GsonUtil;

/**
 * SharedPreferences储存
 *
 * @author liyu
 */
public class DaoSharedPreferences {

    private static DaoSharedPreferences sInstance;

    private SharedPreferences mPreferences;

    private Editor mEditor;

    private static final String NAME = "MemoryDb";

    private static final String TOKEN = "token";

    private static final String USER_INFO = "user_info";//是否第一次点击直播按钮

    private static final String SHOW_AGREE_DIALOG = "show_agree_dialog"; //启动页显示隐私弹框

    private DaoSharedPreferences() {
        mPreferences = App.getInstance().getSharedPreferences(NAME, Context.MODE_PRIVATE);
        mEditor = mPreferences.edit();
    }

    public static DaoSharedPreferences getInstance() {
        if (sInstance == null) {
            sInstance = new DaoSharedPreferences();
        }

        return sInstance;
    }

    /**
     * 获取用户信息
     */
    public String getToken() {
        return mPreferences.getString(TOKEN, "");
    }

    /**
     * 设置用户信息
     *
     * @param token
     */
    public void setToken(String token) {
        mEditor.putString(TOKEN, token);
        mEditor.apply();
    }

    /**
     * 获取用户信息
     */
    public User getUserInfo() {
        User userInfo = null;
        String userInfoStr = mPreferences.getString(USER_INFO, "");
        if (!TextUtils.isEmpty(userInfoStr)) {
            userInfo = GsonUtil.parse(userInfoStr, User.class);
        }
        return userInfo;
    }

    /**
     * 设置用户信息
     *
     * @param person
     */
    public void setUserInfo(User person) {
        if (person == null) {
            return;
        }
        mEditor.putString(USER_INFO, GsonUtil.toJson(person));
        mEditor.apply();
    }

    /**
     * 清除用户信息
     */
    public void clearUserInfo() {
        mEditor.putString(USER_INFO, "");
        mEditor.apply();
    }

    /**
     * 清空数据
     */
    public void clear() {
        mEditor.clear();
        mEditor.apply();
    }

    public boolean getShowAgreeDialog() {
        return mPreferences.getBoolean(SHOW_AGREE_DIALOG, true);
    }

    public void setShowAgreeDialog(boolean showAgreeDialog) {
        mEditor.putBoolean(SHOW_AGREE_DIALOG, showAgreeDialog);
        mEditor.apply();
    }
}
